#
# You will need the DevWisdom carriage plugin to run this.
#
class MigrateLockDownTables < ActiveRecord::Migration
  class << self

    def up
      create_updaters_table
      create_roles_table
      create_users_table
      create_system_objects_table
      create_user_groups_table
      create_system_methods_table
    end

    def down
      drop_system_methods_table
      drop_user_groups_table
      drop_system_objects_table
      drop_users_table
      drop_roles_table
      drop_updaters_table
    end

    private
      def create_updaters_table
        create_table(:updaters) do |t|
          t.column(:name, :string, :null => false)
          t.column(:user_id, :integer)
        end

        Updater.create(:name => "System")
      end

      def drop_updaters_table
        drop_table(:updaters)
      end

      def create_roles_table
        create_table(:roles) do |t|
          t.column(:name, :string, :null => false)
        end
        
        LockDown::ROLES.each do |key,value|
          Role.create(:name => key.to_s.capitalize)
        end
      end

      def drop_roles_table
        drop_table(:roles)
      end

      def create_users_table
        create_table(:users) do |t|
          t.column(:first_name, :string, :limit => 40, :null => false)
          t.column(:middle_name, :string, :limit => 40)
          t.column(:last_name, :string, :limit => 40, :null => false)
          t.column(:login, :string, :limit => 20, :null => false)
          t.column(:password, :string, :limit => 80)
          t.column(:reset_password, :boolean, :default => false)
          t.column(:role_id, :integer)
          t.column(:sid, :string)
          t.column(:reset_request_at, :datetime)
          t.column(:updater_id, :integer, :null => false)
          t.column(:updated_by, :integer, :null => false)
          t.column(:updated_at, :datetime)
        end

        add_index(:users, ["login"], :name => "users_login_key", :unique => true)

        foreign_key(:users, :role_id, :roles)
        foreign_key(:users, :updater_id, :updaters)
        foreign_key(:users, :updated_by, :updaters)
      end

      def drop_users_table
        drop_table(:users)
      end


      def create_system_objects_table
        create_table(:system_objects) do |t|
          t.column(:name, :string, :null => false)
          t.column(:can_remove, :boolean, :default => true)
        end

        create_join_table(:roles, :system_objects)
      end

      def drop_system_objects_table
        drop_join_table(:roles, :system_objects)
        drop_table(:system_objects)
      end

      def create_user_groups_table
        create_table(:user_groups) do |t|
          t.column(:name, :string, :length => 80, :null => false)
          t.column(:role_id, :integer, :null => false)
        end

        create_join_table(:user_groups,:users)
        create_join_table(:system_objects,:user_groups)

        foreign_key(:user_groups, :role_id, :roles)
      end

      def drop_user_groups_table
        drop_join_table(:system_objects,:user_groups)
        drop_join_table(:user_groups,:users)
        drop_table(:user_groups)
      end


      def create_system_methods_table
        create_table(:system_methods) do |t|
          t.column(:name, :string)
          t.column(:system_object_id, :integer, :null => false)
        end

        foreign_key(:system_methods, :system_object_id, :system_objects)
      end

      def drop_system_methods_table
        drop_table(:system_methods)
      end

  end
end
